<?php

namespace App\Repository;

use App\Models\AdminUser as model;

class AdminUserRepository extends  BaseRepository
{
  public function __construct(model $model)
  {
    $this->model = $model;
    parent::__construct();
  }
  public function index($request)
  {
    $where['like'] = ['name', 'phone', 'company_name'];
    $cron['like'] = $this->getWhere($where['like'], $request);
    if (!empty($request->user->company_master_id)) {
      $cron['where'] = $this->getWhere(['company_master_id'], $request);
    }
    if (!empty($request['created_start']) && !empty($request['created_end'])) {
      $cron['between']['create_time'][] = $request['created_start'];
      $cron['between']['create_time'][] = $request['created_end'];
    }
    if (!empty($request->get('roleId'))) {
      $cron['where']['role_id'] = $request->get('roleId');
    }

    $cron['order']['orderby'] = isset($request['orderby']) ? $request['orderby'] : 'id';
    $cron['order']['orderdir'] = isset($request['orderdir']) ? $request['orderdir'] : 'desc';
    $pageNums = 0;
    $cron['limit']['number'] = isset($request['pagenums']) ? ($request['page_size']) : 10;
    if (isset($request['page']) && !empty(($request['page']))) {
      $pageNums = ($request['page'] - 1) * $cron['limit']['number'];
    }
    $cron['limit']['start'] = $pageNums;

    return $this->search($this->model, $cron, 1, 10);
  }
  public function noInList($request)
  {
    $params = $request->all();
    if (!empty($request->get('roleId'))) {
      $cron['where'][] = ['role_id', '!=', $params['roleId']];
    }
    if (!empty($params['userName'])) {
      $cron['like'][] = ['username' => $params['userName']];
    }
    if (!empty($params['phonenumber'])) {
      $cron['where'][] = ['telphone', '=', $params['phonenumber']];
    }

    $cron['order']['orderby'] = isset($request['orderby']) ? $request['orderby'] : 'id';
    $cron['order']['orderdir'] = isset($request['orderdir']) ? $request['orderdir'] : 'desc';
    $pageNums = 0;
    $cron['limit']['number'] = isset($request['pagenums']) ? ($request['page_size']) : 10;
    if (isset($request['page']) && !empty(($request['page']))) {
      $pageNums = ($request['page'] - 1) * $cron['limit']['number'];
    }
    $cron['limit']['start'] = $pageNums;
    return $this->search($this->model, $cron, 1, 10);
  }
  //添加
  public function add($data)
  {
    //必填 字段
    $mustField = [
      'username',
      'userpass'
    ];
    $optionalFields = $this->model->getTableField();
    $msgField = [
      'username' => '账号不能为空',
      'userpass' => '密码不能为空'
    ];
    $insertData  = $this->getRealData($data, $mustField, $optionalFields, $msgField);
    if (!empty($insertData) && is_string($insertData)) {
      //返回错误参数
      return $this->returnMsg(
        2,
        $insertData
      );
    }
    //判断用户是否存在
    $isTrue = $this->model->where(['username' => $insertData['username']])->first();
    if ($isTrue) {
      return $this->returnMsg(3, '该用户已存在');
    }
    //密码加密
    if (!empty($insertData['userpass'])) {
      $passwd = $this->model->passwd($insertData['userpass']);
      $insertData['userpass'] = $passwd['passwd'];
      $insertData['passsalt'] = $passwd['salt'];
    }
    $res = $this->model->insert($insertData);
    if ($res) {
      return $this->returnMsg(200, '添加成功');
    }
    return $this->returnMsg(1, '添加失败');
  }
  //删除
  public function del($where)
  {
    $flag = true;
    $msg = '未知错误';
    $code = 1;
    if (empty($where)) {
      $flag = false;
      $msg = '删除条件不能为空';
      $code = 2;
    }
    if ($flag) {
      $res = $this->model->where($where)->get();
      if (!$res) {
        $flag = false;
        $msg = '未查询到数据';
        $code = 3;
      }
    }
    if ($flag) {
      $del = $this->model->where($where)->delete();
      if ($del) {
        $msg = '删除成功';
        $code = 200;
      }
    }
    return $this->returnMsg($code, $msg);
  }
  //修改
  public function edit($where, $data, $isMust = false)
  {
    if (empty($where)) {
      return $this->returnMsg(1, '条件不能为空');
    }
    if (empty($data)) {
      return $this->returnMsg(2, '修改参数不能为空');
    }
    //过滤字段
    $optionalFields = $this->model->getTableField();
    $field  = $this->getRealData($data, [], $optionalFields, []);
    //用户姓名不能修改
    if (isset($field['username'])) {
      unset($field['username']);
    }
    //密码加密
    if (!empty($field['userpass'])) {
      $passwd = $this->model->passwd($field['userpass']);
      $field['userpass'] = $passwd['passwd'];
      $field['passsalt'] = $passwd['salt'];
    }
    //先查询是否存在
    $res = $this->model->where($where)->update($field);
    if ($isMust && $res) {
      return $this->returnMsg(200, '修改成功');
    }
    if (!$isMust) {
      return $this->returnMsg(200, '修改成功');
    }
    return $this->returnMsg(3, '请重新修改');
  }
  //查看  存入缓存
  public function sel($where = [], $page = 1, $limit = 10, $user = array())
  {
    //过滤查询条件
    //权限限制
    if ($user) {
      $where['liker'] = [
        'dept_str' => $user['dept_str'] . '%'
      ];
    }
    $where['select'] = ['user.*', 'role.name as roel_name'];
    $where['join'] = [
      [
        'join' => 'leftJoin',
        'table' => 'role',
        'left' => 'id',
        'right' => 'user.role_id',
      ]
    ];
    $res = $this->search($this->model, $where, $page, $limit);
    return $res;
  }
  //查询 单条数据
  public function oneSel($where)
  {
    return $this->model->where($where)->first();
  }
  //验证码验证
  public function verifyPasswd($passwd, $numd)
  {
    return $this->model->verifyPasswd($passwd, $numd);
  }
  public function ThisModel()
  {
    return $this->model;
  }
}
